/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.guis.gantt;

import java.util.List;
import java.util.Map;

import org.tbjd.client.constants.GanttConstants;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.dndControllers.gantt.GanttItemDragController;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.interfaces.Builder;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.interfaces.Displayer;
import org.tbjd.client.interfaces.HasDragController;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.interfaces.Observer;
import org.tbjd.client.mediators.AbstractMediator;

import com.allen_sauer.gwt.dnd.client.DragController;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
/**
 * <p>
 * The GanttGui is the panel displaying the Gantt diagramm of the project
 * </p>
 * <p>
 * It superposes the {@link org.tbjd.client.guis.gantt.GanttItemContainerGui GanttItemContainerGui} over the {@link org.tbjd.client.guis.gantt.GanttTimeLineGui GanttTimeLineGui}
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class GanttGui extends Composite implements Builder, Displayer, HasMediator, HasDragController, Decisionmaker, Observer {

	private static GanttGui instance = null;
	private AbsolutePanel container = null;
	private AbstractMediator mediator = null;
	private GanttTimeLineGui ganttTimeLineGui = null;
	private GanttItemContainerGui ganttItemContainerGui = null;
	private GanttItemDragController dragController = null;

	private GanttGui() {
		build();
		initWidget(this.container);
	}

	static public GanttGui getInstance() {
		if (instance == null) {
			instance = new GanttGui();
		}
		return instance;
	}

	public void build() {
		this.container = new AbsolutePanel();
		this.container.setStyleName("gantt");

		this.ganttTimeLineGui = GanttTimeLineGui.getInstance();
		this.ganttItemContainerGui = GanttItemContainerGui.getInstance();
		this.dragController = new GanttItemDragController(this.container, false);
		this.dragController.setBehaviorDragProxy(true);
		this.dragController.setBehaviorDragStartSensitivity(2);
		this.ganttItemContainerGui.setDragController(this.dragController);

		this.container.add(this.ganttTimeLineGui);
		this.container.add(this.ganttItemContainerGui, 0, GanttConstants.TIMELINE_HEADER_HEIGHT);
	}

	public Widget getBuildedWidget() {
		return GanttGui.instance;
	}

	public void display(List<?> items) {
	}

	public void display() {
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
		this.mediator.addObserver(this);
		this.ganttItemContainerGui.setMediator(this.mediator);
	}

	public void decide(Object action) {
		System.out.println("toRemove.decide() TASKITEM_DRAGGING");
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
	}

	public void handle(Object action) {
	}

	/**
	 * This methods is used by the ApplicationLayoutGui to set the boundary panel for the ResourcePickerDragController.
	 * 
	 * @return AbsolutePanel container
	 */
	public AbsolutePanel getContainer() {
		return container;
	}

	public void setDragController(DragController dragController) {
		this.ganttItemContainerGui.setDragController(dragController);
	}

	public void update(Object sender) {
		if (((Map<?, ?>) sender).containsKey(NotificationsConstants.LIST_TASKS)) {
			System.out.println("GanttGui.update() LIST_TASKS");
			this.ganttTimeLineGui.update(sender);
			this.ganttItemContainerGui.update(sender);
		}
		if (((Map<?, ?>) sender).containsKey(NotificationsConstants.TASKMANAGER_RESULT)) {
			System.out.println("GanttGui.update() TASKMANAGER_RESULT");
			this.ganttTimeLineGui.update(sender);
			this.ganttItemContainerGui.update(sender);
		}
	}

}
